﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Jumpton.SFA.Server.BusinessRule;
using BusinessRule.Promotion;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using System.Web.Services;
using Jumpton.SFA.Server.Entity;

public partial class Promotion_PromotionsFormula : PageBase
{
    #region 变量
    public string Promotion_id = "0";
    DataTable dt = null;
    PromotionsManager pm = new PromotionsManager();
    DataSet Forms = null;
    public string ddl_forms = "";
    public string ddl_Promotion = "";
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Write(Request["id"] as string);
        //Response.Write(Request.Form["hidden1"] as string);
        if (!Page.IsPostBack)
        {
            ddl_Promotion = EnumResources.LoadJsonString(EnumType.PromotionType);
            ddl_forms = PromotionsManager.getFormsData(MyStateManager.DomainID);
            pm.Domain_id = MyStateManager.DomainID;
            Promotion_id = Request["id"] as string;
            BindLable();
        }
    }

    public void BindLable()
    {
        if (Promotion_id == "0") return;
        DataSet ds = pm.GetTableData(Promotion_id);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            Label1.Text = ds.Tables[0].Rows[0]["c"].ToString();
            Label2.Text = ds.Tables[0].Rows[0]["n"].ToString();
            Label3.Text = ds.Tables[0].Rows[0]["s"].ToString();
            Label4.Text = ds.Tables[0].Rows[0]["e"].ToString();
            string st = ds.Tables[0].Rows[0]["st"].ToString();
            Label8.Text = (st == "0") ? "未开始" : (st == "1" ? "正在进行" : "已结束");
            Label9.Text = ds.Tables[0].Rows[0]["de"].ToString();
        }
    }

    [WebMethod]
    public static string getFormsToProduct(string f)
    {
        PromotionsManager pm = new PromotionsManager();
        return pm.getProductsData(f);
    }

    /// <summary>
    /// 保存提交
    /// </summary>
    /// <param name="i">自增ID</param>
    /// <param name="d">域ID</param>
    /// <param name="l">用户ID</param>
    /// <param name="pr">活动ID</param>
    /// <param name="ps">促销ID</param>
    /// <param name="sv">促销条件</param>
    /// <param name="su">促销单位</param>
    /// <param name="pp">赠送ID</param>
    /// <param name="pv">赠送条件</param>
    /// <param name="pu">赠送单位</param>
    /// <param name="ht">状态</param>
    /// <returns></returns>
    [WebMethod]
    public static string Svae(int i, decimal d, string l, int pr, int ps, int sv, string su, int pp, int pv, string pu, int ht)
    {
        PromotionsManager manager = new PromotionsManager();
        manager.Domain_id = d;

        PROMOTIONS_PRODUCTS curr_entity = null;
        if (i == -1)
        {
            if (manager.IsExists(pr, ps))
            {
                return "";
            }
            curr_entity = new PROMOTIONS_PRODUCTS();
            curr_entity.DOMAINID = d;
            curr_entity.CREATETIME = BusinessLogic.GetDateTimeLong(DateTime.Now);
        }
        else
        {
            if (manager.IsExists(pr, ps, i))
            {
                return "";
            }
            curr_entity = manager.Load_PP(i);
        }

        if (pr == -1 && ps == -1)
            curr_entity.ACTIVE = false;
        else
        {
            curr_entity.ACTIVE = true;
            curr_entity.PROMOTIONSID = pr;
            curr_entity.PRODUCTSSATISFYID = ps;
            curr_entity.SATISFYVALUE = sv;
            curr_entity.SATISFYUOM = su;
            if (ht == 1)
                curr_entity.PRODUCTSPRESENTID = ps;
            else
                curr_entity.PRODUCTSPRESENTID = pp;
            curr_entity.PRESENTVALUE = pv;
            curr_entity.PRESENTUOM = pu;
            curr_entity.HANDSELTYPE = ht;
        }
        curr_entity.LASTUPDATER = l;
        curr_entity.LASTUPDATETIME = BusinessLogic.GetDateTimeLong(DateTime.Now);

        return manager.Save(curr_entity) ? "1" : "0";
    }
}